package com.beaven.imclient.push;

import com.beaven.imclient.activity.StartActivity;
import com.beaven.imclient.base.MainApplication;
import com.beaven.model.Factory;
import com.beaven.model.data.helper.DbHelper;
import com.beaven.model.data.helper.SessionHelper;
import com.beaven.model.data.helper.UserHelper;
import com.beaven.model.model.api.PushModel;
import com.beaven.model.model.card.GroupCard;
import com.beaven.model.model.card.MessageCard;
import com.beaven.model.model.card.UserCard;
import com.beaven.model.model.db.Group;
import com.beaven.model.model.db.Session;
import com.beaven.model.model.db.User;
import com.beaven.model.model.sp.Account;
import com.raizlabs.android.dbflow.config.FlowManager;
import timber.log.Timber;

import static com.raizlabs.android.dbflow.config.FlowManager.getContext;

/**
 * author: Beaven
 * time: 2017/6/2 16:36
 */

public class PushDispatcher {
  public static void dispatchPush(String str) {
    // 检查登录状态
    if (!Account.isLogin()) {
      return;
    }

    PushModel model = PushModel.decode(str);
    if (model == null) return;

    // 对推送集合进行遍历
    for (PushModel.Entity entity : model.getEntities()) {
      Timber.i("收到的消息--" + entity.toString());
      switch (entity.type) {
        case PushModel.ENTITY_TYPE_LOGOUT:
          Timber.e("推送消息--用户退出--");
          logoutAccount();
          break;
        case PushModel.ENTITY_TYPE_MESSAGE: {
          // 普通消息
          MessageCard card = Factory.getGson().fromJson(entity.content, MessageCard.class);
          Factory.getMessageCenter().dispatch(card);
          break;
        }
        case PushModel.ENTITY_TYPE_ADD_FRIEND:
          // 好友添加
          UserCard userCard = Factory.getGson().fromJson(entity.content, UserCard.class);
          Factory.getUserCenter().dispatchUser(userCard);
          break;
        case PushModel.ENTITY_TYPE_ADD_GROUP:
          // 群组添加
          GroupCard groupCard = Factory.getGson().fromJson(entity.content, GroupCard.class);
          Factory.getGroupCenter().dispatchGroup(groupCard);
          break;
        case PushModel.ENTITY_TYPE_DELETE_FRIEND: {
          // 好友删除
          UserCard card = Factory.getGson().fromJson(entity.content, UserCard.class);
          Timber.e("推送消息--好友删除--" + card.toString());
          DbHelper.instance().deleteModel(User.class, card.build());
          Session session = SessionHelper.findFromLocal(card.getId());
          if (session != null) {
            DbHelper.instance().deleteModel(Session.class, session);
          }
          break;
        }
        case PushModel.ENTITY_TYPE_DELETE_GROUP: {
          // 群删除
          GroupCard card = Factory.getGson().fromJson(entity.content, GroupCard.class);
          Timber.e("推送消息--群组删除--" + card.toString());
          User user = UserHelper.findFromLocal(card.getOwnerId());
          DbHelper.instance().deleteModel(Group.class, card.build(user));
          Session session = SessionHelper.findFromLocal(card.getId());
          if (session != null) {
            DbHelper.instance().deleteModel(Session.class, session);
          }
          break;
        }
        default:
          break;
      }
    }
  }

  private static void logoutAccount() {
    if (Account.clear(getContext())) {
      FlowManager.destroy();
      StartActivity.start(MainApplication.getInstance());
      android.os.Process.killProcess(android.os.Process.myPid());
      System.exit(0);
    }
  }
}
